package com.rj.bd.service;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rj.bd.dao.BookMapper;
import com.rj.bd.entity.Book;
import com.rj.bd.service.solr.BookSolr;
import com.rj.bd.utils.JsonUtil;

@Service
@Transactional //事务
public class BookServiceImpl implements IBookSerivce {

	@Autowired
	public BookMapper bookMapper;

    @Autowired
    public BookSolr bookSolr;
	
	@Override
	public List<Book> queryAll() {
		return bookMapper.queryAll();
	}

    @Override
    public Map<String,Object> getPage(Map maps, String name) throws SolrServerException, IOException {
        System.out.println("pageNo"+maps.get("pageNo")+"\t"+"pageSize"+maps.get("pageSize")+"\t"+"name"+name);

        //分页
        int pageNo=maps.get("pageNo")==null?1:Integer.valueOf(maps.get("pageNo")+"");
        int pageSize=maps.get("pageSize")==null?2:Integer.valueOf(maps.get("pageSize")+"");
//        name= (String) maps.get("name");
//        if (name == null) {
//            name = "";
//        }
//        maps.put("name","%"+name+"%");
        maps.put("start",(pageNo-1)*pageSize);
        maps.put("end",pageSize);

        List<Book> book = null;

        //索引库获取
        System.out.println("去索引库获取");
        book = bookSolr.queryAll(maps,name);

        if(book !=null || book.size()!=0){
            System.out.println("book:"+book+"\t"+"索引库里有:"+book.size());
        }

        //用于模糊查询
        if(name == null || name.equals("")){
            name = "%%";
        }
        else{
            name="%"+name+"%";
        }
        //索引库没有(去mysql查询)
        if(book == null || book.size() == 0){
            System.out.println("索引库没有(去mysql查询)");
            book = bookMapper.getPage(maps,name);
        }

        return  JsonUtil.sendJson(0,"获取成功",null,book);
    }

    @Override
    public List<Book> query(String name) {
        //用于模糊查询
        if(name == null || name.equals("")){
            name = "%%";
        }
        else{
            name="%"+name+"%";
        }
        return bookMapper.query(name);
    }

	@Override
	public void delete(String bid) {
		bookMapper.delete(bid);
	}

	@Override
	public void add(Book book) {
		bookMapper.add(book);
		
	}

	@Override
	public List<Map> queryById(String bid) {
		return bookMapper.queryById(bid);
	}

	@Override
	public void editByZhuangTaiWei(String bid) {
		bookMapper.editByZhuangTaiWei(bid);
	}

	@Override
	public void editByBookZhuangTaiWei(String bid) {
		bookMapper.editByBookZhuangTaiWei(bid);
		
	}




}
